<?php
###
#  Baing PHP 框架 1.0.0
#  http://baing.co/
#
#  Copyright: 白晓阳
#  License: CC BY-NC-SA 4.0
#  https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh
###

namespace bai\action;

use bai\feature\Agent;


class Forward extends \bai\Action
{
    public function run()
    {
        if (self::$request->method !== self::GET) return $this->next();

        $url = '';
        if (self::$request->ajax) {
            $referer = parse_url(Agent::server('HTTP_REFERER'), PHP_URL_QUERY);
            if ($referer) {
                parse_str($referer, $query);
                $url = $query['forward'] ?? '';
            }
        } else {
            $url = self::$request->query['forward'] ?? '';
        }
        if ($url) {
            self::$response->addHeader('Location', $url);
            return $this->exit(quiet: true);
        }

        return $this->next();
    }
}
